home *** CD-ROM | disk | FTP | other *** search
- /*--------------- XDemo.c --------------*/
- /*
- A teeny program that demonstrates the use of external functions.
- By Mark Lankton, 1989, for MacTutor.
- */
-
- #if !defined(USEDUMP)
-
- #include <types.h>
- #include <resources.h>
- #include <quickdraw.h>
- #include <windows.h>
- #include <OSUtils.h>
- #include <OSEvents.h>
- #include <memory.h>
- #include <fonts.h>
- #include <events.h>
- #include <controls.h>
- #include <menus.h>
- #include <dialogs.h>
- #include <desk.h>
- #include <toolutils.h>
- #include <segload.h>
- #include <DiskInit.h>
-
- #if defined(MAKEDUMP)
- #pragma dump "HeaderDumpFile"
- #endif
-
- #else
- #pragma load "HeaderDumpFile"
-
- #endif
-
- #include "XDemo.h"
-
- extern _DataInit();
-
- /*------------- prototypes ----------------*/
- void InitVars(void);
- void SetUpMenus(void);
- void OpenAWindow(void);
- void DoEvents(void);
- void DoActivate(WindowPtr thisWindow,Boolean becomingActive);
- void DoUpdate(WindowPtr thisWindow);
- void Redraw(WindowPtr thisWindow);
- void DoGoAway(WindowPtr thisWindow);
- void DoGrow(WindowPtr thisWindow);
- void DoWindowZoom(WindowPtr thisWindow,short partCode);
- void DoContent(WindowPtr thisWindow);
- void DoKeys(void);
- void DoCommand(long menuResult);
- void DoAbout(void);
- int main(void);
-
- /*Here's the place we'll plug in the external functions. */
- pascal Boolean (*theXTRAProc)(XTRABlock *theBlockPtr);
-
- /*-------------- functions -----------------*/
- void
- InitVars()
- {
- screenRect = qd.screenBits.bounds;
- ClipRect(&screenRect);
-
- SetRect(&dragRect,4,24,screenRect.right-4,
- screenRect.bottom-4);
-
- SetRect(&defaultWRect,screenRect.left,screenRect.top,
- screenRect.left + 500, screenRect.top + 200);
- OffsetRect(&defaultWRect,10,40);
-
- eventRgn = NewRgn(); /*set up an empty region to pass to WaitNextEvent */
- RectRgn(eventRgn,&defaultWRect);
-
- screenDataLength = 512; /*A completely arbitrary value... */
- /*Get a pointer to a little data area and fill it with zeroes. */
- screenData = (short *)NewPtrClear(screenDataLength * 2);
- /*Now a very rudimentary error check...*/
- if(!screenData)
- {
- SysBeep(5);
- ExitToShell();
- }
- }
-
- void
- SetUpMenus()
- {
- int i,howMany;
- short thisID;
- ResType thisType;
- Str255 thisName;
-
- myMenus[appleMenu] = GetMenu(appleID);
- AddResMenu(myMenus[appleMenu], (ResType) 'DRVR');
-
- myMenus[fileMenu] = GetMenu(fileID);
- myMenus[editMenu] = GetMenu(editID);
- myMenus[externalsMenu] = GetMenu(externalsID);
-
- for (i = 1;i <= menuCount;i++)
- InsertMenu(myMenus[i],0);
-
- /*Check for XTRA resources... if any, load them in and plug the names into
- the Externals menu. We use the '1' resource calls because the current resource
- file is XDemo itself.
- */
- howMany = Count1Resources('XTRA');
- if(howMany > 32)
- howMany = 32;
- for(i = 1;i <= howMany;i++)
- {
- XTRAArray[i] = Get1IndResource('XTRA',i);
- if(!XTRAArray[i])
- break;
- MoveHHi(XTRAArray[i]);
- HLock(XTRAArray[i]);
- GetResInfo(XTRAArray[i],&thisID,&thisType,thisName);
- AppendMenu(myMenus[externalsMenu],thisName);
- }
-
- DrawMenuBar();
-
- }
-
- void
- OpenAWindow()
- {
- short width,height;
-
- theWindow = GetNewWindow(128,(Ptr)nil,(WindowPtr)-1);
- SetPort(theWindow);
-
- width = defaultWRect.right - defaultWRect.left;
- height = defaultWRect.bottom - defaultWRect.top;
- SizeWindow(theWindow,width,height,TRUE);
-
- MoveWindow(theWindow,defaultWRect.left,defaultWRect.top,TRUE);
- ShowWindow(theWindow);
-
- /*We'll only have one window at a time, so... */
- DisableItem(myMenus[fileMenu],newItem);
- }
-
-
- void
- DoEvents()
- {
- short whatCode;
- Point dialogPoint;
- Boolean ignoreResult;
- short resultCode;
- WindowPtr whichWindow;
-
- if(!inBackground)
- sleepTime = 0x0A;
- else
- sleepTime = 0xFF;
-
- /*No cursor tricks in this demo, just make sure it's the arrow. */
- InitCursor();
-
- ignoreResult = WaitNextEvent(everyEvent,&myEvent,sleepTime,eventRgn);
- switch (myEvent.what)
- {
- case nullEvent: break;
-
- case mouseDown: whatCode = FindWindow(myEvent.where,&whichWindow);
- switch (whatCode){
- case inMenuBar: DoCommand(MenuSelect(myEvent.where));
- break;
- case inSysWindow: SystemClick(&myEvent,whichWindow);
- break;
- case inDrag: DragWindow(whichWindow,myEvent.where,
- &dragRect);
- break;
-
- case inGoAway: DoGoAway(whichWindow);
- break;
-
- case inGrow: DoGrow(whichWindow);
- break;
-
- case inContent: DoContent(whichWindow);
- break;
-
- case inZoomIn: if (TrackBox(whichWindow,myEvent.where,inZoomIn))
- {
- DoWindowZoom(whichWindow,inZoomIn);
- }
- break;
-
- case inZoomOut: if (TrackBox(whichWindow,myEvent.where,inZoomOut))
- {
- DoWindowZoom(whichWindow,inZoomOut);
- }
- break;
-
- }
- break;
-
- case keyDown:
- case autoKey: DoKeys();
- break;
-
- case activateEvt:
- DoActivate((WindowPtr)myEvent.message,
- myEvent.modifiers & activeFlag);
- break;
-
- case updateEvt: DoUpdate((WindowPtr)myEvent.message);
- break;
-
- case diskEvt: if(HiWord(myEvent.message))
- {
- SetPt(&dialogPoint,85,90);
- resultCode = DIBadMount(dialogPoint,myEvent.message);
- }
- break;
-
-
- case osEvent:
- switch (myEvent.message >> 24)
- {
- case mouseMovedMessage: break;
-
- case suspendResumeMessage:
- inBackground = ((myEvent.message & resumeMask) == 0);
- DoActivate(FrontWindow(), !inBackground);
- break;
- }
- break;
-
- }
-
-
- }
-
-
- void
- DoActivate(thisWindow,becomingActive)
- WindowPtr thisWindow;
- Boolean becomingActive;
- {
-
- SetPort(thisWindow);
-
- if (((WindowPeek)thisWindow)->windowKind >= 8)
- {
- DrawGrowIcon(thisWindow);
-
- if (becomingActive)
- {
- theWindow = thisWindow;
- RectRgn(eventRgn,&(thisWindow->portRect));
- DisableItem(myMenus[editMenu],0);
- }
- else
- EnableItem(myMenus[editMenu],0);
- }
- }
-
- void
- DoUpdate(thisWindow)
- WindowPtr thisWindow;
- {
- GrafPtr savedPort;
-
- GetPort(&savedPort);
- SetPort(thisWindow);
-
- if (((WindowPeek)thisWindow)->windowKind >= 8)
- {
- ClipRect(&screenRect);
- BeginUpdate(thisWindow);
- EraseRect(&thisWindow->portRect);
- DrawGrowIcon(thisWindow);
- Redraw(thisWindow);
- EndUpdate(thisWindow);
- }
- SetPort(savedPort);
- }
-
- void
- Redraw(thisWindow)
- WindowPtr thisWindow;
- {
- GrafPtr savedPort;
- Rect localRect;
- short center;
- int i;
-
- GetPort(&savedPort);
- SetPort(thisWindow);
-
- localRect = thisWindow->portRect;
- localRect.right -= scrollBarAdjust;
- localRect.bottom -= scrollBarAdjust;
- ClipRect(&localRect);
-
- center = localRect.bottom / 2;
- MoveTo(0,center);
- PenPat(qd.gray);
- Line(localRect.right,0);
- PenPat(qd.black);
- MoveTo(0,center);
-
- for(i = 0; i < screenDataLength;i++)
- LineTo(i,center - *(screenData + i));
-
- SetPort(savedPort);
- }
-
- void
- DoGoAway(thisWindow)
- WindowPtr thisWindow;
- {
- /*
- Our windows have the default windowKind (8)... don't
- try to close someone else's window!
- */
- if(((WindowPeek)thisWindow)->windowKind < 8)
- return;
-
- DisposeWindow(thisWindow);
-
- if(GetNextEvent(everyEvent,&myEvent))
- DoActivate((WindowPtr)myEvent.message,myEvent.modifiers & activeFlag);
- if(GetNextEvent(everyEvent,&myEvent))
- DoActivate((WindowPtr)myEvent.message,myEvent.modifiers & activeFlag);
-
- /*Turn the "New" item back on so we
- can make another window if we want. */
- EnableItem(myMenus[fileMenu],newItem);
- }
-
- void
- DoGrow(thisWindow)
- WindowPtr thisWindow;
- {
- Rect sizeRect;
- long newSize;
- int newWidth, newHeight;
-
- if (thisWindow != FrontWindow())
- SelectWindow(thisWindow);
- else
- {
- SetRect(&sizeRect, minWidth,minHeight,screenRect.right,
- screenRect.bottom - mBarHeight);
- newSize = GrowWindow(thisWindow,myEvent.where,&sizeRect);
- if (newSize)
- {
- newWidth = LoWord(newSize);
- newHeight = HiWord(newSize);
- SizeWindow(thisWindow,newWidth,newHeight,TRUE);
-
- InvalRect(&(thisWindow->portRect));
- }
- }
- }
-
- void
- DoWindowZoom(thisWindow,partCode)
- WindowPtr thisWindow;
- short partCode;
- {
-
- EraseRect(&(thisWindow->portRect));
- ZoomWindow(thisWindow,partCode,TRUE);
-
- InvalRect(&thisWindow->portRect);
- }
-
- void
- DoContent(thisWindow)
- WindowPtr thisWindow;
- {
- /*Not much action here... */
- if (thisWindow != FrontWindow())
- SelectWindow(thisWindow);
- }
-
- void
- DoKeys()
- {
- short temp;
- long menuChoice;
-
- /*Only worry about command-key stuff. */
- temp = (short)(myEvent.message & charCodeMask);
- if (myEvent.modifiers & cmdKey)
- {
- if (myEvent.what != autoKey)
- {
- menuChoice = MenuKey(temp);
- DoCommand(menuChoice);
- }
- }
- }
-
- void
- DoCommand(menuResult)
- long menuResult;
- {
- Str255 name;
- short theMenu,theItem;
- XTRABlock theBlock;
- Boolean theResult;
-
- theMenu = HiWord(menuResult); /*Gives you the resource ID. */
- theItem = LoWord(menuResult);
-
- switch (theMenu){
- case appleID:
- if (theItem == aboutMeItem)
- {
- DoAbout();
- break;
- }
- else EnableItem(myMenus[3],undoItem);
- GetItem(myMenus[1], theItem, name);
- (void)OpenDeskAcc(name);
- break;
-
- case fileID:
- switch (theItem)
- {
- case newItem: OpenAWindow();
- break;
-
- case closeItem: DoGoAway(FrontWindow());
- break;
-
- case quitItem: allDone = TRUE;
- break;
- }
- break;
-
- /*We don't use the Edit menu at all. */
- case editID: break;
-
- case externalsID: theBlock.dataLength = screenDataLength;
- theBlock.theData = screenData;
- (ProcPtr)theXTRAProc = (ProcPtr)*(XTRAArray[theItem]);
- theResult = (*theXTRAProc)(&theBlock);
- if(!theResult)
- /*Do something here */
- ;
- InvalRect(&(theWindow->portRect));
- break;
-
- }
- HiliteMenu(0);
- }
-
- void
- DoAbout()
- {
- DialogPtr thisDialog;
- GrafPtr savedPort;
- Boolean wait = TRUE;
- Point thePoint;
-
- GetPort(&savedPort);
- thisDialog = GetNewDialog(aboutID,nil,(WindowPtr)-1);
- SetPort(thisDialog);
-
- DrawDialog(thisDialog);
-
- while (wait)
- {
- if (GetNextEvent(mDownMask,&myEvent))
- {
- thePoint = myEvent.where;
- GlobalToLocal(&thePoint);
- if (PtInRect(thePoint,&thisDialog->portRect))
- wait = FALSE;
- else
- SysBeep(5);
- }
- }
-
- SetPort(savedPort);
- DisposDialog(thisDialog);
-
- }
-
- int
- main()
- {
- UnloadSeg(_DataInit);
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent,0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- InitVars();
- SetUpMenus();
- OpenAWindow();
-
- do {
- DoEvents();
- } while (allDone == FALSE);
-
-
-
- ExitToShell();
-
- }